﻿using KinonekoSoftware.CodariaDB.Documents.TemplateContents;

namespace KinonekoSoftware.Studio.Pages.Instances.Templated
{
    [NeedLocalised]
    [DataReference(typeof(Rarity))]
    [DataReference(typeof(Element))]
    [DataReference(typeof(Vocabulary))]
    [DataReference(typeof(TheoryEngine_Element))]
    [DataReference(typeof(TheoryEngine_Rarity))]
    [DataReference(typeof(TheoryEngine_Vocabulary))]
    [Operation(ResourceType.Avatar)]
    public sealed class AbilityTemplatedEditorViewModel : TabViewModelCore
    {

        private readonly TheoryEngine_Element    _theory_element;
        private readonly TheoryEngine_Rarity     _theory_rarity;
        private readonly TheoryEngine_Vocabulary _theory_vocabulary;

        private readonly RepositoryPropertyManager _PropertyMgr;
        private readonly TemplatedContentEngine    _engine_templated;
        private readonly InstanceCacheEngine       _engineInstanceCache;
        private readonly InstanceEngine            _engine_instance;
        private readonly GravatarSystem            _engine_image;



        private Rarity               _rarity;
        private Element              _element;
        private bool                 _showElementIcon;
        private bool                 _enableLeftTopMaskLayer;
        private bool                 _enableRightBottomMaskLayer;
        private TemplatedContentSize _contentSize;
        private bool                 _isUltimate;

        private string _elementIcon;
        private int    _rarityLevel;
        private int    _ver_rarity;
        private int    _ver_element;


        public AbilityTemplatedEditorViewModel() : base()
        {
            Platform.GetEngine<
                TemplatedContentEngine,
                InstanceCacheEngine,
                InstanceEngine,
                GravatarSystem,
                TheoryEngine_Element,
                TheoryEngine_Rarity,
                TheoryEngine_Vocabulary>(
                                         out var runtime,
                                         out _engine_templated,
                                         out _engineInstanceCache,
                                         out _engine_instance,
                                         out _engine_image,
                                         out _theory_element,
                                         out _theory_rarity,
                                         out _theory_vocabulary);

            var repository = runtime.Repository;
            _PropertyMgr = repository.PropertyManager;

            Keywords     = new ObservableCollection<ColoredKeyword>();
            Vocabularies = new ObservableCollection<ColoredVocabulary>();
            Rarities     = new ObservableCollection<Rarity>();
            Elements     = new ObservableCollection<Element>();
        }

       

        protected override void OnResume()
        {
            if (_theory_rarity.Version != _ver_rarity)
            {
                Rarities.AddMany(_theory_rarity.Collection, true);
                _ver_rarity = _theory_rarity.Version;
            }

            if (_theory_element.Version != _ver_element)
            {
                Elements.AddMany(_theory_element.Collection, true);
                _ver_element = _theory_element.Version;
            }

            base.OnResume();
        }





        /// <summary>
        /// 获取或设置 <see cref="IsUltimate"/> 属性。
        /// </summary>
        public bool IsUltimate
        {
            get => _isUltimate;
            set
            {
                SetValue(ref _isUltimate, value);
                SetDirtyState(true);
            }
        }

        /// <summary>
        /// 获取或设置 <see cref="EnableRightBottomMaskLayer"/> 属性。
        /// </summary>
        public bool EnableRightBottomMaskLayer
        {
            get => _enableRightBottomMaskLayer;
            set
            {
                SetValue(ref _enableRightBottomMaskLayer, value);
                _PropertyMgr.EnableAbilityRightBottomMaskLayer = value;
            }
        }

        /// <summary>
        /// 获取或设置 <see cref="EnableLeftTopMaskLayer"/> 属性。
        /// </summary>
        public bool EnableLeftTopMaskLayer
        {
            get => _enableLeftTopMaskLayer;
            set
            {
                SetValue(ref _enableLeftTopMaskLayer, value);
                _PropertyMgr.EnableAbilityLeftTopMaskLayer = value;
            }
        }

        /// <summary>
        /// 获取或设置 <see cref="ShowElementIcon"/> 属性。
        /// </summary>
        public TemplatedContentSize ContentSize
        {
            get => _contentSize;
            set
            {
                SetValue(ref _contentSize, value);
                _PropertyMgr.AbilityTemplatedContentSize = value;
                PushToView(value);
            }
        }



        public bool ShowElementIcon
        {
            get => _showElementIcon;
            set
            {
                SetValue(ref _showElementIcon, value);
                _PropertyMgr.EnableAbilityElementIcon = value;
            }
        }


        public Element Element
        {
            get => _element;
            set
            {
                SetValue(ref _element, value);
                SetDirtyState(true);

                if (_element is not null)
                {
                    ElementIcon = _element.Icon;
                }
            }
        }


        public Rarity Rarity
        {
            get => _rarity;
            set
            {
                SetValue(ref _rarity, value);
                SetDirtyState(true);


                if (_rarity is not null)
                {
                    RarityLevel = _rarity.Level;
                }
                else
                {
                    RarityLevel = 1;
                }
            }
        }




        public int RarityLevel
        {
            get => _rarityLevel;
            private set => SetValue(ref _rarityLevel, value);
        }


        public string ElementIcon
        {
            get => _elementIcon;
            private set => SetValue(ref _elementIcon, value);
        }

        public ObservableCollection<ColoredKeyword>    Keywords     { get; }
        public ObservableCollection<ColoredVocabulary> Vocabularies { get; }
        public ObservableCollection<Rarity>            Rarities     { get; }
        public ObservableCollection<Element>           Elements     { get; }


        public ICommandEX SelectAvatarCommand { get; }
        public ICommandEX ResetAvatarCommand  { get; }

        public ICommandEX NewKeywordCommand          { get; }
        public ICommandEX EditKeywordCommand         { get; }
        public ICommandEX ShiftUpKeywordCommand      { get; }
        public ICommandEX ShiftDownKeywordCommand    { get; }
        public ICommandEX RemoveKeywordCommand       { get; }
        public ICommandEX NewVocabularyCommand       { get; }
        public ICommandEX EditVocabularyCommand      { get; }
        public ICommandEX ShiftUpVocabularyCommand   { get; }
        public ICommandEX ShiftDownVocabularyCommand { get; }
        public ICommandEX RemoveVocabularyCommand    { get; }
    }
}